<?php
/*
Author: Hallvard Westman
Project: Links
 ---------------------------------USER PAGE FOR LINKS----------------------------------------*/

session_start();

include 'db.php';
include 'userclass.php';
include 'functions.php';


/*---------------------------------INSERTING LINK----------------------------------------*/	
		
		$UserId=$_GET['id'];
		
		if (isset ($_POST['Url'])) {
		$link = $_POST['Url'];
		if(substr($link, 0, 4)!="http") 
			$link = "http://".$link;
		
		$sql = 'INSERT INTO li_links (UserId,Url)
					VALUES(:UserId, :Url)';
			
			$sth = $db->prepare($sql);
			$sth->bindParam(':UserId',$UserId);
			$sth->bindParam(':Url',$link);
			$res = $sth->execute();
			if(!$res){
			echo "<a>Error while inserting</a>";
			header('location: links.php?id='.$UserId);
			}
			header('location: links.php?id='.$UserId);
		}		
		
/*---------------------------------DISPLAYS LINKS----------------------------------------*/
		
		include 'header.php';
		include 'bar.php';
		
		
		$sql = 'SELECT Url FROM li_links WHERE UserId =:UserId ORDER BY Count DESC';
			$sth = $db->prepare($sql);
			$sth->bindParam(':UserId', $UserId);
			$sth->execute();
			
		
		for ($i=1;$i<=8;$i++){
			
			if ($row = $sth->fetch()){
		
			$url=$row['Url'];
			?>	
			<div id="frame">
			<div id="totallink">											
			<a href="link.php?link=<?php echo $url; ?>"><div id="link"></div></a>
			<div id="pic">
			<script type="text/javascript">
				wsr_snapshot('<?php echo $url; ?>', '71639ScRsWSf', 's');
			</script>
			</div>
			</div>
			<div id="sidebar" onmouseover="javascript : sidebar()">
			
			
			
			<!--
			<ul>
				<li><img src="symbols/stick.png"/></li>
				<li><img src="symbols/edit.png"/></li>
				<li><img src="symbols/delete.png"/></li>
			</ul>	
			-->	
			
			</div>
			<a href="<?php echo $url; ?>"><?php echo $url; ?></a>
			
			</div>
			
		<?php }  }   	
/*---------------------------------FORM FOR LINK INSERT----------------------------------------*/
		
		
		$id = $user->getID();	//gets logged inn userid
		if(isset($_GET['id'])){
		$get_id = $_GET['id'];	//gets blog userid
		
		//check to see if user is logged in as correct user
		if($user->loggedOn()&&$id==$get_id){ 
		?>
		<div id="url">	
		<form method="post" action="" enctype="multipart/form-data">
		<ul>
		<!--<li><label for="Url">URL</label></li>-->
		<li><input type="text" name="Url" value="Url"/></li>
		<li><button type="submit">Add</button></li>
		
		</ul>
		
		</form>
		</div>
		
<?php
}
}
include 'footer.php';

